package com.zego.wawaji.ui.center.pay.bill;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.utils.StringUtils;
import com.zego.wawaji.ui.base.ExBaseAdapter;
import com.zego.wawaji.ui.center.pay.bean.OrderRecord;

import java.util.List;

/**
 * 充值记录
 * Created by Su on 2017/11/27.
 */
public class RechargeAdapter extends ExBaseAdapter<OrderRecord> {
    public RechargeAdapter(Context context, List<OrderRecord> datas) {
        super(context, datas);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder holder;
        if (convertView == null) {
            convertView = inflate(R.layout.my_recharge_record_item);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }
        final OrderRecord info = getItem(position);
        holder.type_icon.setImageResource(getRechargeType(info));
        holder.type_descl.setText(info.getRemark());
        holder.time_info.setText(StringUtils.getCreateTime(info.getCreateTime()));
        holder.recharge_desc.setText(info.getRemark());
        payStatus(info, holder.pay_suc, holder.pay_status);

        holder.ll_gold.setVisibility(View.GONE);
        holder.ll_coin.setVisibility(View.GONE);
        if (info.getWwCoinCz() > 0) {
            holder.ll_gold.setVisibility(View.VISIBLE);
            holder.recharge_gold.setText("+" + (info.getWwCoinCz() + info.getWwCoinZs()));
        } else if (info.getWwCoinZs() > 0) {
            holder.ll_coin.setVisibility(View.VISIBLE);
            holder.recharge_num.setText("+" + info.getWwCoinZs());
        }

        return convertView;
    }

    private void payStatus(OrderRecord info, LinearLayout paySuc, TextView payStatus) {
        if (info.getPayStatus() >= 0) {  // 成功: 积极状态
            paySuc.setVisibility(View.VISIBLE);
            payStatus.setVisibility(View.GONE);
            return;
        }
        paySuc.setVisibility(View.GONE);
        payStatus.setVisibility(View.VISIBLE);
        payStatus.setText("未支付");
    }

    private int getRechargeType(OrderRecord info) {
        int res = R.drawable.recharge_wx;
        switch (info.getPayType()) {
            case 1: // 微信
                res = R.drawable.recharge_wx;
                break;

            case 2:  // 支付包
                res = R.drawable.recharge_alipay;
                break;

            case 3:  // 娃娃兑换
                res = R.drawable.recharge_wawa;
                break;

            case 4:  // 积分兑换
                res = R.drawable.recharge_point;
                break;

            case 5:  // 邀请奖励
                res = R.drawable.recharge_invite;
                break;

            case 6:  // 签到奖励
                res = R.drawable.recharge_sign;
                break;

            case 7:  // 意外补偿
                res = R.drawable.recharge_patch;
                break;

            case 8: // 分享奖励
                res = R.drawable.recharge_share;
                break;

            case 9: // 注册奖励
                res = R.drawable.recharge_register;
                break;
        }
        return res;
    }

    private class MyViewHolder {
        private ImageView type_icon;
        private TextView type_descl, time_info, recharge_num, recharge_gold, recharge_desc, pay_status;
        private LinearLayout pay_suc;
        private RelativeLayout ll_coin, ll_gold;

        public MyViewHolder(View convertView) {
            initView(convertView);
        }

        private void initView(View convertView) {
            type_icon = (ImageView) convertView.findViewById(R.id.type_icon);
            type_descl = (TextView) convertView.findViewById(R.id.type_descl);
            time_info = (TextView) convertView.findViewById(R.id.time_info);
            recharge_num = (TextView) convertView.findViewById(R.id.recharge_num);
            recharge_desc = (TextView) convertView.findViewById(R.id.recharge_desc);
            pay_suc = (LinearLayout) convertView.findViewById(R.id.pay_suc);
            pay_status = (TextView) convertView.findViewById(R.id.pay_status);
            ll_coin = (RelativeLayout) convertView.findViewById(R.id.ll_coin);
            ll_gold = (RelativeLayout) convertView.findViewById(R.id.ll_gold);
            recharge_gold = (TextView) convertView.findViewById(R.id.recharge_gold);
        }
    }
}